package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.GradeAuthZdxbMapper;
import com.zhijian.medical.entity.po.GradeAuthZdxb;
import com.zhijian.medical.entity.vo.request.GradeAuthZdxbSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.GradeAuthZdxbSearchVo;
import com.zhijian.medical.entity.vo.response.GradeAuthZdxbVo;
import com.zhijian.medical.service.GradeAuthZdxbService;
import com.zhijian.util.AssertUtil;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 分级授权书-诊断细胞业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2024-11-14 10:40:51
 */
@Slf4j
@Service
public class GradeAuthZdxbServiceImpl implements GradeAuthZdxbService {

	@Autowired
    private GradeAuthZdxbMapper mapper;

    @Override
    public GradeAuthZdxb selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public GradeAuthZdxbVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(GradeAuthZdxb gradeAuthZdxb) {
        Date now = new Date();
        gradeAuthZdxb.setCreateTime(now);
        gradeAuthZdxb.setUpdateTime(now);
        return mapper.insert(gradeAuthZdxb) > 0 ? gradeAuthZdxb.getId() : 0L;
    }

    @Override
    public Long insertSelective(GradeAuthZdxb gradeAuthZdxb) {
        Date now = new Date();
        gradeAuthZdxb.setCreateTime(now);
        gradeAuthZdxb.setUpdateTime(now);
        return mapper.insertSelective(gradeAuthZdxb) > 0 ? gradeAuthZdxb.getId() : 0L;
    }

    @Override
    public boolean update(GradeAuthZdxb gradeAuthZdxb) {
        Date now = new Date();
        gradeAuthZdxb.setUpdateTime(now);
        return mapper.updateByPrimaryKey(gradeAuthZdxb) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(GradeAuthZdxb record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(GradeAuthZdxbSaveOrUpdateVo saveOrUpdateVo) {
        GradeAuthZdxb gradeAuthZdxb = new GradeAuthZdxb();
        BeanUtils.copyProperties(saveOrUpdateVo, gradeAuthZdxb);
        return this.insert(gradeAuthZdxb) != 0;
    }

    @Override
    public boolean modify(GradeAuthZdxbSaveOrUpdateVo saveOrUpdateVo) {
        GradeAuthZdxb old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        GradeAuthZdxb gradeAuthZdxb = new GradeAuthZdxb();
        BeanUtils.copyProperties(saveOrUpdateVo, gradeAuthZdxb);
        return this.update(gradeAuthZdxb);
    }

    @Override
    public GradeAuthZdxbVo selectOne(GradeAuthZdxbSearchVo gradeAuthZdxbSearchVo) {
        return mapper.selectOne(gradeAuthZdxbSearchVo);
     }

    @Override
    public Integer count(GradeAuthZdxbSearchVo gradeAuthZdxbSearchVo) {
        return mapper.count(gradeAuthZdxbSearchVo);
    }

    @Override
    public List<GradeAuthZdxbVo> selectList(GradeAuthZdxbSearchVo gradeAuthZdxbSearchVo) {
        return mapper.selectList(gradeAuthZdxbSearchVo);
    }

    @Override
    public Page<GradeAuthZdxbVo> selectPage(GradeAuthZdxbSearchVo gradeAuthZdxbSearchVo) {
        AssertUtil.check(Objects.isNull(gradeAuthZdxbSearchVo.getPageNum()) || Objects.isNull(gradeAuthZdxbSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(gradeAuthZdxbSearchVo);
        //分页
        Page<GradeAuthZdxbVo> page = new Page<>(true, count, gradeAuthZdxbSearchVo.getPageNum(), gradeAuthZdxbSearchVo.getPageSize());
        gradeAuthZdxbSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<GradeAuthZdxbVo> responseVos = this.selectList(gradeAuthZdxbSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<GradeAuthZdxb> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(GradeAuthZdxbSearchVo gradeAuthZdxbSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(gradeAuthZdxbSearchVo);
    }
    
}